#include "includes.h"
#include "zephyr/sys/printk.h"

LOG_MODULE_REGISTER(Key);
#define KEY_GPIOA DT_ALIAS(sw0)
#define KEY_GPIOB DT_ALIAS(sw1)
#define KEY_GPIOX DT_ALIAS(sw2)
#define KEY_GPIOY DT_ALIAS(sw3)

static const struct gpio_dt_spec button_A = GPIO_DT_SPEC_GET_OR(KEY_GPIOA, gpios, {0});
static const struct gpio_dt_spec button_B = GPIO_DT_SPEC_GET_OR(KEY_GPIOB, gpios, {0});
static const struct gpio_dt_spec button_X = GPIO_DT_SPEC_GET_OR(KEY_GPIOX, gpios, {0});
static const struct gpio_dt_spec button_Y = GPIO_DT_SPEC_GET_OR(KEY_GPIOY, gpios, {0});
static struct gpio_callback button_cb_data_A;
static struct gpio_callback button_cb_data_B;
static struct gpio_callback button_cb_data_C;
static struct gpio_callback button_cb_data_D;
extern  const struct gpio_dt_spec led;
void button_pressed(const struct device *dev, struct gpio_callback *cb,
		    uint32_t pins)
{
    gpio_pin_toggle_dt(&led);
	LOG_INF("button press\r\n");
    
}



void key_init(void)
{
    int ret;
    if((!gpio_is_ready_dt(&button_A)) ||
      (!gpio_is_ready_dt(&button_B)) ||
      (!gpio_is_ready_dt(&button_X)) ||
      (!gpio_is_ready_dt(&button_Y))
    )
    {
        LOG_INF("Error: button device %s is not ready\n",button_A.port->name);
        LOG_INF("Error: button device %s is not ready\n",button_B.port->name);
        LOG_INF("Error: button device %s is not ready\n",button_X.port->name);
        LOG_INF("Error: button device %s is not ready\n",button_Y.port->name);
         return ;
    }
    ret = gpio_pin_configure_dt(&button_A, GPIO_INPUT);
    if(ret != 0)
    {
        LOG_INF("Error: button device %s is not ready\n",button_A.port->name);
    }
    ret = gpio_pin_configure_dt(&button_B, GPIO_INPUT);
    if(ret != 0)
    {
        LOG_INF("Error: button device %s is not ready\n",button_B.port->name);
    }
    ret = gpio_pin_configure_dt(&button_X, GPIO_INPUT);
    if(ret != 0)
    {
        LOG_INF("Error: button device %s is not ready\n",button_X.port->name);
    }
    ret = gpio_pin_configure_dt(&button_Y, GPIO_INPUT);
    if(ret != 0)
    {
        LOG_INF("Error: button device %s is not ready\n",button_Y.port->name);
    }

    ret = gpio_pin_interrupt_configure_dt(&button_A, GPIO_INT_EDGE_TO_INACTIVE);
	if (ret != 0) {
        LOG_INF("Error: button device %s is not ready\n",button_A.port->name);
	}
    ret = gpio_pin_interrupt_configure_dt(&button_B, GPIO_INT_EDGE_TO_INACTIVE);
    if (ret != 0) {
        LOG_INF("Error: button device %s is not ready\n",button_B.port->name);
	}
    ret = gpio_pin_interrupt_configure_dt(&button_X, GPIO_INT_EDGE_TO_INACTIVE);
    if (ret != 0) {
        LOG_INF("Error: button device %s is not ready\n",button_X.port->name);
	}
    ret = gpio_pin_interrupt_configure_dt(&button_Y, GPIO_INT_EDGE_TO_INACTIVE);
        if (ret != 0) {
        LOG_INF("Error: button device %s is not ready\n",button_Y.port->name);
	}
    gpio_init_callback(&button_cb_data_A, button_pressed, BIT(button_A.pin));
	gpio_init_callback(&button_cb_data_B, button_pressed, BIT(button_B.pin));
    gpio_init_callback(&button_cb_data_C, button_pressed, BIT(button_X.pin));
    gpio_init_callback(&button_cb_data_D, button_pressed, BIT(button_Y.pin));

    gpio_add_callback(button_A.port, &button_cb_data_A);
    gpio_add_callback(button_B.port, &button_cb_data_B);
    gpio_add_callback(button_X.port, &button_cb_data_C);
    gpio_add_callback(button_Y.port, &button_cb_data_D);
	printk("Set up button at %s pin %d\n", button_A.port->name, button_A.pin);
    printk("Set up button at %s pin %d\n", button_B.port->name, button_B.pin);
    printk("Set up button at %s pin %d\n", button_X.port->name, button_X.pin);
    printk("Set up button at %s pin %d\n", button_Y.port->name, button_Y.pin);
    return ;
}

void key_task(void) {


    key_init();

    while (1)
    {
        gpio_pin_get_dt(&button_A);
        gpio_pin_get_dt(&button_B);
        gpio_pin_get_dt(&button_X);
        gpio_pin_get_dt(&button_X);
        k_msleep(20);
    }
    

    // if (val_A >= 0) {
    //     gpio_pin_set_dt(&button_A, val_A);
    // }   

    // if (val_B >= 0) {
    //     gpio_pin_set_dt(&button_B, val_B);
    // } 

    // if (val_X >= 0) {
    //     gpio_pin_set_dt(&button_X, val_X);
    // }
    // if (val_Y >= 0) {
    //     gpio_pin_set_dt(&button_Y, val_Y);
    // }
}

K_THREAD_DEFINE(key_id, 1024, key_task, NULL, NULL, NULL, 
                7, 0, 0);
